package com.example.qxfw.shfx.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.qxfw.common.entity.yj.SlWarning;
import com.example.qxfw.common.entity.zy.BaseProduct;
import com.example.qxfw.mongodb.hy.entity.HyDocFile;
import com.example.qxfw.ybzz.mapper.BaseProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import com.example.qxfw.shfx.Mapper.SlWarningMapper;
import com.example.qxfw.shfx.Service.YjcxService;

import java.sql.Timestamp;
import java.util.List;


@Service
public class YjcxServiceImpl implements YjcxService {
    @Autowired
    private SlWarningMapper slWarningMapper;

    @Autowired
    private BaseProductMapper baseProductMapper;

    @Qualifier("mongoSecondTemplate")
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<SlWarning> selectByTimePeriodOfSlWarning(Timestamp startDate, Timestamp endDate, Integer state) {
        return slWarningMapper.selectList(new LambdaQueryWrapper<SlWarning>()
                .between(SlWarning::getDatechar, startDate, endDate)
                .eq(ObjectUtils.isNotEmpty(state),SlWarning::getState,state)
                .orderByDesc(SlWarning::getDocid));
    }

    @Override
    public SlWarning updateState(int docId, int state) {
        SlWarning slWarning = new SlWarning();
        slWarning.setDocid(docId);
        slWarning.setState(state);
        slWarningMapper.updateById(slWarning);
        return slWarningMapper.selectById(docId);
    }

    @Override
    public List<SlWarning> deleteByIdOfSlWarning(int docId) {
        List<SlWarning> deleteByIdOfSlWarning = slWarningMapper.selectList(new LambdaQueryWrapper<SlWarning>()
                .eq(SlWarning::getDocid, docId));
        slWarningMapper.delete(new LambdaQueryWrapper<SlWarning>()
                .eq(SlWarning::getDocid, docId));
        if (deleteByIdOfSlWarning.size()>0){
            List<BaseProduct> deleteByIdProduct = baseProductMapper.selectList(Wrappers.<BaseProduct>lambdaQuery()
                    .eq(BaseProduct::getBindid,deleteByIdOfSlWarning.get(0).getDocid())
                    .eq(BaseProduct::getProductname,deleteByIdOfSlWarning.get(0).getDocpath()));
            baseProductMapper.delete(Wrappers.<BaseProduct>lambdaQuery()
                    .eq(BaseProduct::getBindid,deleteByIdOfSlWarning.get(0).getDocid())
                    .eq(BaseProduct::getProductname,deleteByIdOfSlWarning.get(0).getDocpath()));
            if (deleteByIdProduct.size()>0){
                mongoTemplate.remove(new Query(
                        Criteria.where("id").is(deleteByIdProduct.get(0).getId())
                                .and("table_name").is("base_product")), HyDocFile.class);
            }
        }
        return deleteByIdOfSlWarning;
    }

    @Override
    public Integer getProductId(Integer docId, String docPath) {
        List<BaseProduct> baseProducts = baseProductMapper.selectList(Wrappers.<BaseProduct>lambdaQuery()
                .eq(BaseProduct::getBindid, docId)
                .eq(BaseProduct::getProductname, docPath));
        if (baseProducts.size()>0){
            return baseProducts.get(0).getId();
        } else {
            return null;
        }

    }


}
